print("hello")
Welcome to the interactive ML workshop!
In this workshop we will do "computer vision", one particular area of machine learning
This is good to work with interactively because you can see with your own eyes what is going on in the neural network...
... all concepts apply to other areas of machine learning, and artificial intelligence more broadly
From vision, to speech, text, and numbers
#Let's start by setting up the environment that we will use during this workshop
import os, requests, json, os.path
from fastai.vision import *
from fastai import *
from fastai.callbacks.hooks import *
#from google_images_download import google_images_download
#response = google_images_download.googleimagesdownload()
from PIL import Image as pil_image
PIL.Image.LOADTRUNCATEDIMAGES = True
Ok, so what do we want to classify on?
os.chdir('C:/images/')
os.makedirs('classes', exist_ok=True)
path = 'C:/images/classes/'
class_list = [(f,f.replace('.csv','')) for f in os.listdir(path) if '.csv' in f]
for _,c in class_list:
loc = os.path.join(path,c)
if not os.path.isdir(loc):
os.mkdir(loc)
print(class_list)
for f,c in class_list:
print(f,c)
csv = os.path.join(path,f)
print(csv)
download_images(csv,os.path.join(path,c))
verify_images(os.path.join(path,c), delete=True, max_size=500)
# do we have enough data?
DIR = 'C:/images/classes/'
d ={name:len(os.listdir(os.path.join(DIR,name))) for name in os.listdir(DIR) if not os.path.isfile(os.path.join(DIR, name))}
for k,v in d.items():
print(k,':',v)
print('')
# prepping the data...
np.random.seed(42)
data = ImageDataBunch.from_folder("C:/images/", train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)
# is everything ok?
data.show_batch(rows=3, figsize=(7,8))
len(data.train_ds), len(data.valid_ds)
# so here we choose the model architecture!
learn = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy])
learn.load('pre_trained_chp')
learn.fit_one_cycle(2)
learn.validate()
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
interp.plot_top_losses(9)
# so what would happen with random weights, instead of starting with a pre-trained network (transfer learning)?
learn.save('pre_trained_chp')
del learn
learn_wo = cnn_learner(data, models.resnet34, metrics=[error_rate,accuracy], pretrained=False)
learn_wo.fit_one_cycle(4)
interp_wo = ClassificationInterpretation.from_learner(learn_wo)
# not so good in comparison
interp.plot_confusion_matrix()
plt.figure()
interp_wo.plot_confusion_matrix()
interp_wo.plot_top_losses(9)
learn_wo.save('trained')
learn.load('pre_trained')
m = learn_wo.model.eval();
idx=18
x,y = data.valid_ds[idx]
x.show()
x,_ = data.one_item(x)
x_im = Image(data.denorm(x)[0])
#Image(data.denorm(x)[0])
def hooked_backward(cat=y):
with hook_output(m[0]) as hook_a:
with hook_output(m[0], grad=True) as hook_g:
preds = m(x)
preds[0,int(cat)].backward()
return hook_a,hook_g
hook_a,hook_g = hooked_backward()
acts = hook_a.stored[0].cpu()
acts.shape
avg_acts = acts.mean(0)
avg_acts.shape
def show_heatmap(hm):
_,ax = plt.subplots()
x_im.show(ax)
ax.imshow(hm, alpha=0.6, extent=(0,224,224,0),
interpolation='bilinear', cmap='magma');
show_heatmap(avg_acts)